﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECCIT.Data;
using System.Data;
using System.Data.SqlClient;

using ECCIT.Business.Model;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.DAL
{
    public class EventSponsorLogoDAL
    {
        //插入新记录
        public bool Add(EventSponsorLogoModel model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@"insert into UKNOW_EVENTS_SPONSOR_LOGO(COL_LANG_ID,COL_EVENTS_ID,COL_LOGO_NAME,COL_LOGO_PATH,COL_LOGO_DESC,COL_LOGO_ORDER,COL_LOGO_INSERT_TIME,COL_LOGO_UPDATE_TIME) 
                            values (@COL_LANG_ID,@COL_EVENTS_ID,@COL_LOGO_NAME,@COL_LOGO_PATH,@COL_LOGO_DESC,@COL_LOGO_ORDER,@COL_LOGO_INSERT_TIME,@COL_LOGO_UPDATE_TIME )");
            IDataParameter[] parameters = {
					new SqlParameter("@COL_LANG_ID", SqlDbType.Int),
					new SqlParameter("@COL_EVENTS_ID", SqlDbType.Int),
					new SqlParameter("@COL_LOGO_NAME", SqlDbType.NVarChar,200),
                    new SqlParameter("@COL_LOGO_PATH", SqlDbType.NVarChar,200),
					new SqlParameter("@COL_LOGO_DESC", SqlDbType.NVarChar,200),
					new SqlParameter("@COL_LOGO_ORDER", SqlDbType.Int),
                    new SqlParameter("@COL_LOGO_INSERT_TIME", SqlDbType.DateTime),
					new SqlParameter("@COL_LOGO_UPDATE_TIME", SqlDbType.DateTime)
                                          };

            parameters[0].Value = model.COL_LANG_ID;
            parameters[1].Value = model.COL_EVENTS_ID;
            parameters[2].Value = model.COL_LOGO_NAME;
            parameters[3].Value = model.COL_LOGO_PATH;
            parameters[4].Value = string.IsNullOrEmpty(model.COL_LOGO_DESC) ? "无描述" : model.COL_LOGO_DESC;
            parameters[5].Value = model.COL_LOGO_ORDER;
            parameters[6].Value = model.COL_LOGO_INSERT_TIME;
            parameters[7].Value = model.COL_LOGO_UPDATE_TIME;

            return DBHelper.ExecuteNonQuery(strSql.ToString(), parameters) > 0;
        }

        //根据langID和EventsID获取数据记录,显示列表
        public DataTable getSponLogoData(int langID, int? EventsID)
        {
            string sqlcmd = String.Format("select * from dbo.UKNOW_EVENTS_SPONSOR_LOGO where COL_LANG_ID ={0} and COL_EVENTS_ID={1} ", langID, EventsID);

            DataSet ds = DBHelper.ExecuteDataSet(sqlcmd, null);

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                return dt;
            }
            else
            {
                return null;
            }
        }
            //仅根据赞助商LogoID来查询一条数据记录
        public EventSponsorLogoModel GetSponLogoModel( int LogoID)
        {
            string sqlcmd = string.Format("select * from UKNOW_EVENTS_SPONSOR_LOGO where COL_LOGO_ID={0}",  LogoID);
            DataSet ds = DBHelper.ExecuteDataSet(sqlcmd, null);
            if (ds.Tables[0].Rows.Count>0)
            {
                EventSponsorLogoModel cem = new EventSponsorLogoModel();
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cem.COL_EVENTS_ID = Convert.ToInt32(dr["COL_EVENTS_ID"]);
                    cem.COL_LANG_ID = Convert.ToInt32(dr["COL_LANG_ID"]);
                    cem.COL_LOGO_ID = Convert.ToInt32(dr["COL_LOGO_ID"]);
                    cem.COL_LOGO_NAME = dr["COL_LOGO_NAME"].ToString();
                    cem.COL_LOGO_PATH = dr["COL_LOGO_PATH"].ToString();
                    cem.COL_LOGO_ORDER = Convert.ToInt32(dr["COL_LOGO_ORDER"]);
                    cem.COL_LOGO_DESC = dr["COL_LOGO_DESC"].ToString();
                    if (dr["COL_LOGO_INSERT_TIME"] != DBNull.Value) cem.COL_LOGO_INSERT_TIME = DateTime.Parse(dr["COL_LOGO_INSERT_TIME"].ToString());
                    if (dr["COL_LOGO_UPDATE_TIME"] != DBNull.Value) cem.COL_LOGO_UPDATE_TIME = DateTime.Parse(dr["COL_LOGO_UPDATE_TIME"].ToString());
                    
                }
                return cem;
            }
            else
            {
                return null;
            }
        }

        //仅根据赞助商LogoID来删除数据记录,LogoID是主键
        public bool delete( int LogoID)
        {
            string sqlcmd = String.Format("delete from  UKNOW_EVENTS_SPONSOR_LOGO where COL_LOGO_ID={0}",  LogoID);
            int i = DBHelper.ExecuteNonQuery(sqlcmd);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        //仅根据LogoID来修改编辑页面中要修改的字段
        public bool Update(EventSponsorLogoModel model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update UKNOW_EVENTS_SPONSOR_LOGO set COL_LOGO_NAME=@COL_LOGO_NAME,COL_LOGO_PATH=@COL_LOGO_PATH,COL_LOGO_ORDER=@COL_LOGO_ORDER ,COL_LOGO_UPDATE_TIME=@COL_LOGO_UPDATE_TIME where COL_LOGO_ID=@COL_LOGO_ID");
            IDataParameter[] parameters = {
                    new SqlParameter("@COL_LOGO_NAME", SqlDbType.NVarChar,200),
                    new SqlParameter("@COL_LOGO_PATH", SqlDbType.NVarChar,200),
                    new SqlParameter("@COL_LOGO_ORDER", SqlDbType.Int),
                    new SqlParameter("@COL_LOGO_ID", SqlDbType.Int),
                    new SqlParameter("@COL_LOGO_INSERT_TIME", SqlDbType.DateTime),
                    new SqlParameter("@COL_LOGO_UPDATE_TIME", SqlDbType.DateTime)
                                         };

            parameters[0].Value = model.COL_LOGO_NAME;
            parameters[1].Value = model.COL_LOGO_PATH;
            parameters[2].Value = model.COL_LOGO_ORDER;
            parameters[3].Value = model.COL_LOGO_ID;
            parameters[4].Value = model.COL_LOGO_INSERT_TIME;
            parameters[5].Value = model.COL_LOGO_UPDATE_TIME;

            return DBHelper.ExecuteNonQuery(strSql.ToString(), parameters) > 0;

        }
    }
}
